#pragma once

#include<iostream>

using namespace std;



template<class K,class V>
struct BSTreeNode
{
public:
	BSTreeNode(const K& _key,const V& _val) :
		key(_key),
		left(nullptr),
		right(nullptr),
		val(_val)
	{}

	BSTreeNode<K,V>* left;
	BSTreeNode<K,V>* right;
	K key;
	V val;
};

template<class K,class V>
class BSTree
{
	typedef BSTreeNode<K,V> node;
public:

	bool insert(const K& key,const V& val)
	{
		node* prev = _root;
		node* now = _root;
		if (_root == nullptr)
		{
			_root = new node(key,val);
			return true;
		}

		while (now)
		{
			if (key > now->key)
			{
				prev = now;
				now = now->right;
			}
			else if (key < now->key)
			{
				prev = now;
				now = now->left;
			}
			else
			{
				return false;
			}
		}

		now = new node(key,val);
		if (key > prev->key)
		{
			prev->right = now;
		}
		else
		{
			prev->left = now;
		}
		return true;

	}

	void InOrder()
	{
		_InOrder(_root);
	}

	node* find(const K& key)
	{
		
		node* now = _root;

		while (now)
		{
			if (now->key > key)
			{
				now = now->left;
			}
			else if (now->key < key)
			{
				now = now->right;
			}
			else
			{
				return now;
			}
		}
		return nullptr;

	}

	bool Erase(const K& key)
	{
		node* now = _root;
		node* prev = nullptr;

		while (now)
		{
			if (now->key > key)
			{
				prev = now;
				now = now->left;
			}
			else if (now->key < key)
			{
				prev = now;
				now = now->right;
			}
			else
			{
				if (now->left == nullptr)
				{
					if (now == _root)
					{
						_root = now->right;
					}
					else
					{
						if (prev->left == now)
						{
							prev->left = now->right;
						}
						else if(prev->right == now)
						{
							prev->right = now->right;
						}
					}
					delete now;
				}
				else if (now->right == nullptr)
				{
					if (now == _root)
					{
						_root = now->left;
					}
					else
					{
						if (prev->left == now)
						{
							prev->left = now->left;
						}
						else if (prev->right == now)
						{
							prev->right = now->left;
						}
					}
					delete now;
				}
				else
				{
					node* subleft = now->right;
					node* parent = now;
					while (subleft->left)
					{
						parent = subleft;
						subleft = subleft->left;
					}

					swap(subleft->key, now->key);

					if (subleft == parent->left)
					{
						parent->left = subleft->right;
					}
					else if(subleft == parent->right)
					{
						parent->right = subleft->right;
					}

					delete subleft;
				}
				return true;
			}
		}
		return false;

	}

	~BSTree()
	{
		Destory(_root);
	}

	BSTree() {}

	BSTree(BSTree<K,V>& k)
	{
		_root = copy(k._root);
	}

	BSTree<K,V>& operator=(BSTree<K,V> k)
	{
		swap(_root = k._root);
		return *this;
	}

private:
	node* _root = nullptr;

	node* copy(node* root)
	{
		if (root == nullptr)
			return nullptr;

		node* newnode = new node(root->key);
		newnode->left = copy(root->left);
		newnode->right = copy(root->right);
		return newnode;
	}

	void Destory(node* root)
	{
		if (root == nullptr)
			return;

		Destory(root->left);
		Destory(root->right);
		delete root;
	}

	void _InOrder(node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->left);
		cout << root->key << ":" << root->val << endl;;
		_InOrder(root->right);
	}
};
